/**
 * Title: TitleBarView.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-9-4 下午5:57:24
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.widgets.titlebar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.ui.common.listener.OnClickAvoidForceListener;

/** 
 * ClassName: TitleBarView
 * Description:标题栏
 * Create by 罗旭东
 * Date 2013-9-4 下午5:57:24
 */
public class TitleBarView extends RelativeLayout{
	/**
	 * 标题栏基本样式
	 */
	public static final int MODEL_NORMAL = 0;
	
	/**
	 * 只有返回按钮的样式
	 */
	public static final int MODEL_BACK = 1;
	
	/**
	 * 搜索按钮
	 */
	public static final int MODEL_SEARCH = 6;
	
	/**
	 * 包含返回跟创建文件夹按钮
	 */
	public static final int MODEL_BACK_CREATE = 7;
	
	private ITitleBarOnClickListener titleBarOnClickListener = null;
	private ITitleBarSearchListener titleBarSearchListener = null;
	private RelativeLayout titleLayout = null;
	private LinearLayout normalTitleLayout = null;
	private LinearLayout searchTitleLayout = null;
	private ImageView backBtn = null;
	private ImageView leftLineImageView = null;
	private TextView contentTextView = null;
	private LinearLayout rightLayout = null;
	private ImageView rightImageView = null;
	
	private int titleMode = MODEL_NORMAL;
	
	public TitleBarView(Context context) {
		super(context);
		init(context);
	}
	
	public TitleBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	public TitleBarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public ITitleBarOnClickListener getTitleBarOnClickListener() {
		return titleBarOnClickListener;
	}

	private void init(Context context)
	{
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		titleLayout = (RelativeLayout)inflater.inflate(R.layout.title_bar, null);
		
		backBtn = (ImageView)titleLayout.findViewById(R.id.iv_title_back);
		leftLineImageView = (ImageView)titleLayout.findViewById(R.id.iv_left_spe);
		contentTextView = (TextView)titleLayout.findViewById(R.id.tv_title_name);
		rightLayout = (LinearLayout)titleLayout.findViewById(R.id.ll_right_btn);
		rightImageView = (ImageView)titleLayout.findViewById(R.id.iv_title_right);
		normalTitleLayout = (LinearLayout)titleLayout.findViewById(R.id.ll_title_normal);
		searchTitleLayout = (LinearLayout)titleLayout.findViewById(R.id.ll_title_search);
		
		backBtn.setOnClickListener(onClickListener);
		rightImageView.setOnClickListener(onClickListener);
		addView(titleLayout);
		changeMode(MODEL_NORMAL);
	}
	
	public void setRequestFocus(){
	}
	
	public void setTitleBarOnClickListener(
			ITitleBarOnClickListener titleBarOnClickListener) {
		this.titleBarOnClickListener = titleBarOnClickListener;
	}
	
	public void setTitleBarSearchListener(
			ITitleBarSearchListener titleBarSearchListener) {
		this.titleBarSearchListener = titleBarSearchListener;
	}
	
	private void showSearchTitleLayout()
	{
		normalTitleLayout.setVisibility(View.GONE);
		searchTitleLayout.setVisibility(View.VISIBLE);
	}
	
	private void hiddenSearchTitleLayout()
	{
		normalTitleLayout.setVisibility(View.VISIBLE);
		searchTitleLayout.setVisibility(View.GONE);
	}
	
	public void setTitle(int resId)
	{
		contentTextView.setText(resId);
	}
	
	public void setTitle(String title)
	{
		contentTextView.setText(title);
	}
	
	public void changeMode(int model)
	{
		switch (model) {
		case MODEL_NORMAL:
			change2NormalModel();
			break;
		case MODEL_BACK:
			change2BackModel();
			break;
		case MODEL_SEARCH:
			change2SearchModel();
			break;
		default:
			break;
		}
	}
	
	private void change2NormalModel()
	{
		hiddenSearchTitleLayout();
		backBtn.setVisibility(View.GONE);
		leftLineImageView.setVisibility(View.GONE);
		rightLayout.setVisibility(View.GONE);
	}
	
	private void change2BackModel()
	{
		hiddenSearchTitleLayout();
		backBtn.setVisibility(View.VISIBLE);
		leftLineImageView.setVisibility(View.VISIBLE);
		rightLayout.setVisibility(View.GONE);
	}
	
	private void change2SearchModel()
	{
		hiddenSearchTitleLayout();
		backBtn.setVisibility(View.GONE);
		leftLineImageView.setVisibility(View.GONE);
		rightLayout.setVisibility(View.VISIBLE);
		rightImageView.setImageResource(R.drawable.title_search_button);
	}
	
	private OnClickAvoidForceListener onClickListener = new OnClickAvoidForceListener() {

		@Override
		public void onClickAvoidForce(View v) {
			switch (v.getId()) {
			case R.id.iv_title_back:
				if (titleBarOnClickListener != null)
				{
					titleBarOnClickListener.onLeftClick(v);
				}
				break;
			case R.id.iv_title_right:
				if (titleBarOnClickListener != null)
				{
					titleBarOnClickListener.onRightClick(v);
				}
				break;
			default:
				break;
			}
		}
		
	};
}
